$(document).ready(function() {

	//首次加载时获取pid_select下拉框内容
	changeProject();

	//设置datepicker
	$("#meeting_task_start_time").datepicker({
		showAnim: "blind",
		dateFormat: "yy-mm-dd",
		changeYear: true,
		changeMonth: true,
		monthNamesShort: ["1月", "2月", "3月", "4月", "5月", "6月", "7月", "8月", "9月", "10月", "11月", "12月"],
		dayNamesMin: ['日', "一", "二", "三", "四", "五", "六"],
		onClose: function( selectedDate ) {
        $( "#meeting_task_end_time" ).datepicker( "option", "minDate", selectedDate );
      }
	});
	$("#meeting_task_end_time").datepicker({
		showAnim: "blind",
		dateFormat: "yy-mm-dd",
		changeYear: true,
		changeMonth: true,
		monthNamesShort: ["1月", "2月", "3月", "4月", "5月", "6月", "7月", "8月", "9月", "10月", "11月", "12月"],
		dayNamesMin: ['日', "一", "二", "三", "四", "五", "六"],
		onClose: function( selectedDate ) {
        $( "#meeting_task_start_time" ).datepicker( "option", "maxDate", selectedDate );
      }
	});
	
});

//项目切换事件
function changeProject() {
	var pid = $("#pid_select").val();
	//首次加载时获取select下拉框内容
	$.ajax({
		type: "get",
		dataType: "json",
		cache: false,
		url: "getPhasesByPid",
		data: {
			pid: pid
		},
		success: function(data) {
			//获取阶段下拉框
			var obj = $("#phaseId_select");
			//首先清空option选项
			obj.empty();
			//处理数据展示
			var len = data.length;
			if(len > 0) {
				obj.append("<option disabled selected value='1'>--请选择--</option>");
				for(var i = 0; i < len; i++) {
					obj.append("<option value=" + data[i].phaseId + ">" + data[i].phaseName + "</option>");
				}
			}
			obj.val(1);
			//phaseId_select的option切换事件
			obj.change(function() {
				changePhase($(this).val());
			});
			//			$("#deptId").val(1);
			//调用异步ajax提交数据,默认显示编号为1的部门
			//			getPointByDept(1);
		}
	});
}

//项目阶段切换事件
function changePhase(phaseId) {
	var pid = $("#pid_select").val();
	$.ajax({
		type: "get",
		dataType: "json",
		cache: false,
		url: "getProjectPhaseById",
		data: {
			pid: pid,
			phaseId: phaseId
		},
		success: function(data) {
			console.log(data);
			if(data.length > 0) {
				$("#phasePlanStartTime").text(data[0].phasePlanStartTime.substr(0, 10));
				$("#phasePlanEndTime").text(data[0].phasePlanEndTime.substr(0, 10));
				$("#phasePoint").text(data[0].phasePoint);
			}
		}
	});
}
/**
 * 
 */
function beforeSubmit(){
	var phaseId = $('#phaseId_select');
	if(phaseId.val() == null){
		$("#check_phaseId").text("请选择阶段");
		return false;
	}
	var taskName = $('input[name="taskName"]');
	if(taskName.val() == ""){
		$("#check_taskName").text("请输入任务名称！");
		taskName.focus();
		return false;
	}
	var taskDetail = $('#task_detail');
	if(taskDetail.val() == ""){
		$("#check_detail").text("请输入任务详情！");
		taskDetail.focus();
		return false;
	}
	var taskRequirement = $('#taskRequirement');
	if(taskRequirement.val() == ""){
		$("#check_taskRequest").text("请输入任务要求！");
		taskRequirement.focus();
		return false;
	}
	var taskPlanStartTime = $('#meeting_task_start_time');
	if(taskPlanStartTime.val() == ""){
		$("#check_meeting_task_start_time").text("请输入任务开始时间！");
		taskPlanStartTime.focus();
		return false;
	}
	var taskPlanEndTime = $('#meeting_task_end_time');
	if(taskPlanEndTime.val() == ""){
		$("#check_meeting_task_end_time").text("请输入任务结束时间！");
		taskPlanEndTime.focus();
		return false;
	}
	var taskTotalPoint = $('input[name="taskTotalPoint"]');
	
	if(taskTotalPoint.val() == ""){
		$("#check_task_point").text("请输入任务分数！");
		taskTotalPoint.focus();
		return false;
	}else{
		if(!isNaN(taskTotalPoint.val())){
			//console.log($("#deptTaskPoint").text());
			var pp = parseInt($("#phasePoint").text());
			var ps = parseInt(taskTotalPoint.val());
			console.log(pp);
			console.log(ps);
			if(ps > pp){
				$("#check_task_point").text("任务分数大于可以上限，请重新输入分数");
				return false;
		}
		}else{
			$("#check_task_point").text("请输入数字格式的分数！");
			return false;
		}
	}
	return true;
}
